package alkaid.eis.sys.service;

import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import alkaid.eis.sys.entity.Dict;
import alkaid.eis.sys.entity.DictDetail;
import lombok.experimental.UtilityClass;

/**
 * 字典查询条件
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@UtilityClass
public class DictSpecifications {
    public Specification<Dict> empty() {
        return (Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            return null;
        };
    }

    public Specification<Dict> codeIn(Set<String> codeSet) {
        return (Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            return cb.in(root.get("code")).value(codeSet);
        };
    }

    public static Specification<Dict> codeEqual(String code) {
        return (Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            return cb.equal(root.get("code"), code);
        };
    }

    public static Specification<Dict> labelLike(String label) {
        return (Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            Join<Dict, DictDetail> joinDetail = root.join("details", JoinType.INNER);
            return cb.like(joinDetail.get("label"), "%" + label + "%");
        };
    }

    public static Specification<Dict> descriptionLike(String description) {
        return (Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            return cb.like(root.get("description"), "%" + description + "%");
        };
    }    
}
